<template>
  <div class="app-container">
    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearcf"
      :close-on-click-modal="false"
      draggable
      width="780px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="98px"
      >
        <el-col :span="12">
          <el-form-item label="出库日期">
            <el-date-picker
              v-model="dateRange"
              size="small"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              style="width: 215px"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="领料单号" prop="cfmDjbh">
            <el-input
              v-model="queryParams.cfmDjbh"
              placeholder="请输入领料单号"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="出库仓库" prop="ckDm">
            <el-select
              v-model="queryParams.ckDm"
              style="width: 580px"
              multiple
              clearable
              placeholder="请选择出库仓库"
            >
              <el-option
                clearable
                v-for="dict in allStorageOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-col :span="24">
            <el-form-item label="综合查询" prop="desc">
              <el-input
                style="width: 580px"
                v-model="queryParams.desc"
                clearable
                size="small"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              style="margin-top: 25px; display: flex; justify-content: flex-end"
            >
              <el-button @click="cancel">取消</el-button>
              <el-button type="primary" @click="handleQuery">确认</el-button>
            </el-form-item>
          </el-col>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-searcf"
          @click="handleSearcf"
          size="small"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['output:cfmReport:export']"
          @click="handleExcel(1, '五金领料明细')"
          :loading="exportLoading"
          size="small"
        >
          明细数据
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['output:cfmReport:export']"
          @click="handleExcel(2, '物料统计')"
          size="small"
        >
          物料统计
        </el-button>
      </el-col>
      <!-- 分页效果 -->
      <el-col :span="2.2" style="position: absolute; right: 0px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-tabs v-model="activeTab">
      <el-tab-pane label="明细数据" name="first">
        <el-table
          highlight-current-row
          border
          stripe
          :height="tableHcfild"
          :loading="loading"
          :data="cfdDetailList"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="领料单号"
            align="center"
            width="135"
            prop="cfmDjbh"
          />
          <el-table-column
            label="部门名称"
            width="100"
            :show-overflow-tooltip="true"
            align="center"
            prop="cfmBmdm"
          />
          <el-table-column
            label="仓库"
            class-name="rkck"
            width="100"
            :show-overflow-tooltip="true"
            align="center"
            prop="cfmCkdm"
          >
            <template #default="scope">
              <dict-tag
                :options="allStorageOptions"
                :value="scope.row.cfmCkdm"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="物料代码"
            width="135"
            align="center"
            prop="cfdWldm"
          />
          <el-table-column
            label="品名规格"
            width="285"
            show-overflow-tooltip
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="80"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="最小包装"
            width="80"
            align="center"
            prop="cfdMinbz"
          />
          <el-table-column
            label="申请数量"
            width="80"
            align="center"
            prop="cfdSqsl"
          />
          <el-table-column
            label="出库数量"
            width="80"
            align="center"
            prop="cfdCksl"
          />
          <el-table-column
            label="存放位置"
            width="100"
            align="center"
            prop="cfdCwdm"
          >
            <template #default="scope">
              <dict-tag :options="allCwOptions" :value="scope.row.cfdCwdm" />
            </template>
          </el-table-column>
          <el-table-column
            label="备注"
            width="80"
            align="center"
            prop="cfmDesc"
          />
          <el-table-column
            label="开单人员"
            width="100"
            :show-overflow-tooltip="true"
            align="center"
            prop="cfmJlry"
          />
          <el-table-column
            label="开单日期"
            :show-overflow-tooltip="true"
            align="center"
            width="150"
            prop="cfmJlrq"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.cfmJlrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="审核人员"
            width="80"
            :show-overflow-tooltip="true"
            align="center"
            prop="cfmShry"
          />
          <el-table-column
            label="审核日期"
            width="150"
            :show-overflow-tooltip="true"
            align="center"
            prop="cfmShrq"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.cfmShrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="出库人员"
            width="80"
            :show-overflow-tooltip="true"
            align="center"
            prop="cfdCkry"
          />
          <el-table-column
            label="出库日期"
            width="150"
            :show-overflow-tooltip="true"
            align="center"
            prop="cfdCkrq"
          />
          <el-table-column
            label="过账人员"
            width="80"
            :show-overflow-tooltip="true"
            align="center"
            prop="cfmGzry"
          />
          <el-table-column
            label="过账日期"
            width="150"
            :show-overflow-tooltip="true"
            align="center"
            prop="cfmGzrq"
          />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="按物料统计" name="second">
        <el-table
          border
          stripe
          @scrollBottom="load2"
          :height="tableHcfild"
          :data="cfdWldmList"
          :cell-style="styleCell"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column label="物料代码" align="center" prop="cfdWldm" />
          <el-table-column label="品名规格" align="center" prop="itmPmgg" />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column label="申请数量" align="center" prop="cfdSqsl" />
          <el-table-column label="出库数量" align="center" prop="cfdCksl" />
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { getAll, getstorageType } from "@/api/repertory/Storage";
import { getAllCustomerTwo } from "@/api/project/customer";
import { getReport, ReportExcel } from "@/api/output/cfm";
import { clmList } from "@/api/output/clmReport";

export default {
  name: "cfmReport",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      tableHcfild: document.body.clientHeight - 280,
      activeTab: "first",
      cfdDetailList: [],
      cfdDateList: [],
      cfdCusList: [],
      cfdWldmList: [],
      // 显示搜索条件
      openSearcf: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      total2: 0,
      total3: 0,
      total4: 0,
      //有效存储仓库
      storageOptions: [],
      //所以存储仓库
      allStorageOptions: [],
      cfmTypeOptions: [],
      //所有客户代码字典
      allCustomerOptions: [],
      //所有出库类型字典
      allOutTypeOptions: [],
      //所有存储储位
      allCwOptions: [],
      //单位字典
      unitFlagOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 建立日期时间范围
      dateRange: [
        this.$moment().startOf("month").toDate(),
        this.$moment().endOf("month").toDate(),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 25,
        cfdWldm: null,
        cfmDjbh: null,
        ckDm: [],
        index: 0,
        desc: null,
      },
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    //单位
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    getstorageType("0").then((response) => {
      this.storageOptions = response.data;

      for (let item of this.storageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    getAll().then((response) => {
      this.allCwOptions = response.data;

      for (let item2 of this.allCwOptions) {
        item2.dictLabel = item2.storageDefaultstorage;

        item2.listClass = "default";
        item2.dictValue = item2.storageId + "";
      }
    });
    //领料类型
    this.getDicts("outsourcing_requisition_type").then((response) => {
      this.cfmTypeOptions = response.data;
    });
    getAll().then((response) => {
      this.allStorageOptions = response.data;

      for (let item of this.allStorageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
      // this.allStorageOptions.push(data)
    });
    getAllCustomerTwo().then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.cusName;
        i.dictValue = i.cusId + "";
        i.listClass = "default";
      }
      this.allCustomerOptions = response.data;
    });
    //所有出货类型
    this.getAllDicts("sys_out_type").then((response) => {
      this.allOutTypeOptions = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询来料进货入库开单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.dateRange && "" != this.dateRange) {
        this.queryParams.params["begincfdCkrq"] = this.moment(
          this.dateRange[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endcfdCkrq"] = this.moment(
          this.dateRange[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      getReport(this.queryParams).then((response) => {
        this.cfdDetailList = [];
        this.cfdWldmList = [];
        this.cfdDateList = [];
        this.cfdCusList = [];
        //明细
        this.cfdDetailList = this.cfdDetailList.concat(
          response.data.list1.rows
        );
        this.total = response.data.list1.total;
        //物料
        this.cfdWldmList = this.cfdWldmList.concat(response.data.list2.rows);
        this.total2 = response.data.list2.total;
        // //日期
        // this.cfdDateList = this.cfdDateList.concat(response.data.date.rows)
        // this.total3 = response.data.date.total
        // //仓库
        // this.cfdCusList = this.cfdCusList.concat(response.data.cus.rows)
        // this.total4 = response.data.stroage.total
        this.loading = false;
      });
    },
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 40;
        this.queryParams.pageNum++;
        this.getList();
      }
      return 0;
    },
    load2() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total2 / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 20;
        this.queryParams.pageNum++;
        this.getList();
      }
      return 0;
    },
    // load2() {
    //   if (this.queryParams.pageNum < Math.ceil(this.total2 / this.queryParams.pageSize)) {
    //     this.queryParams.pageNum++
    //     getReport(this.queryParams).then((response) => {
    //       //按物料统计
    //       this.chdWldmList = this.chdWldmList.concat(response.data.list2.rows)
    //       this.total2 = response.data.list2.total
    //       this.loading = false
    //     })
    //   }
    //   return 0
    // },

    handleSearcf() {
      this.openSearcf = true;
      this.title = "查询五金仓领料";
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearcf = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearcf = false;
    },
    //导出Excel
    handleExcel(index) {
      this.queryParams.index = index;
      const queryParams = this.queryParams;
      this.$confirm("确认要导出五金仓领料统计分析吗？", "确认", {
        cancelButtonText: "取消",
        confirmButtonText: "确认",
      })
        .then(() => {
          this.exportLoading = true;
          return ReportExcel(queryParams);
        })
        .then((res) => {
          this.download(res.msg);
          this.queryParams.index = 0;
          this.exportLoading = false;
        })
        .catch(() => {
          this.exportLoading = false;
        });
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    styleCell({ row, column, rowIndex, columnIndex }) {
      if (row[column.property] && row[column.property] < 0)
        return { color: "red" };
    },
  },
};
</script>
